<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:20
 */

class GoldController extends AdminBaseController {

    /**
     * 货币流向报表
     */
    public function index(){
        $this->setTitle(PubFunc::t('货币流向报表'));

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $coinTypes = PubDict::getDict($this->game, 'cointype');
        $flowTypes = PubDict::getDict($this->game, 'FlowType');

        $filter->addPlatformFilter()
            ->addServerFilter()
            ->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',strtotime('yesterday')), 'date' => 'day'))
            ->addFilter('coinType', 'select', PubFunc::t('货币'), array('options' => $coinTypes, 'value' => key($coinTypes)))
            ->addFilter('flowType', 'radio', PubFunc::t('途径'), array('options' => $flowTypes, 'value' => key($flowTypes)));

        $params = $filter->get();
        $header = GoldService::indexHeader();
        $data   = GoldService::indexData($params);

        $grid->addGrid($header, $data, array(
            'order' => array('ActionCoin', 'desc'),
        ));

        $chartConfig = GoldService::indexChart($data, $params);
        $chart->addChart('get_chart', $chartConfig['data'], $chartConfig['config']);

        $this->out['filter'] = $filter->show();
        $this->out['chart'] = $chart->show();
        $this->out['grid'] = $grid->show();
    }

    public function analysis(){
        $this->setTitle('货币产出消耗数据分析');

        $filter = new PubFilter('pl');
        $grid   = new PubGrid();

        $coinTypes = PubDict::dict('roma', 'cointype');

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-31 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('coinType', 'radio', PubFunc::t('货币'), array('options' => $coinTypes, 'value' => key($coinTypes)))
            ->addExport()
        ;

        $params = $filter->get();
        $params['period'] = 'Daily';

        $info = GoldService::analysisInfo($params);
        $header = $info['header'];
        $data   = $info['data'];

        if($filter->isExport()) {
            $filename = LibExcel::getFilename('', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'name'  => '货币产出消耗数据分析',
                'order' => array('RecDate', 'desc')
            ));

            $this->out['filter']    = $filter->show();
            $this->out['grid']      = $grid->show();
        }
    }

} 